//
//  JustTest.m
//  RegexDemo
//
//  Created by 周文成 on 2018/1/4.
//  Copyright © 2018年 C-LOVE. All rights reserved.
//

#import "JustTest.h"

@implementation JustTest

+ (void)test {
    
    
    
    [self lasteMore];
//    [self CMPhoneNumber];
//    [self is11Number];
//    [self once];
//    [self runoAskD];
//    [self runoAnyD];
//    [self runoAndD];
//    [self huakuohao];
}



+ (void)lasteMore {
    NSString *regex = @"^a.*";
    NSString *s1 = @"a2";
    NSString *s2 = @"b1";
    NSString *s3 = @"a";
    NSString *s4 = @"asdfdgfdgfdg";
    
    NSPredicate *pre = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    NSLog(@"正则表达式:%@ %@ %@", regex, s1, [pre evaluateWithObject:s1]?@"符合":@"不符合");
    NSLog(@"正则表达式:%@ %@ %@", regex, s2, [pre evaluateWithObject:s2]?@"符合":@"不符合");
    NSLog(@"正则表达式:%@ %@ %@", regex, s3, [pre evaluateWithObject:s3]?@"符合":@"不符合");
    NSLog(@"正则表达式:%@ %@ %@", regex, s4, [pre evaluateWithObject:s4]?@"符合":@"不符合");
    
}


/**
 * 中国移动：China Mobile
 * 移动：134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188,1705
 */
+ (void)CMPhoneNumber {
    /** 匹配前4个数字 */
    NSString *regex = @"^1(34[0-8]|(3[5-9]|5[017-9]|8[278])\\d|705)";
    
    //解释:开始 1 (34[0到8] 或 (3[5到9] 或 5[0或1或(7到9)] 或 8[2或7或8])一位数字 或 705 )
    
    NSString *s1 = @"1347";
    NSString *s2 = @"1349";
    NSString *s3 = @"1359";
    NSString *s4 = @"1883";
    NSString *s5 = @"1705";
    NSString *s6 = @"1992";
    
    NSPredicate *pre = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    NSLog(@"正则表达式:%@ %@ %@", regex, s1, [pre evaluateWithObject:s1]?@"是移动号码":@"不是移动号码");
    NSLog(@"正则表达式:%@ %@ %@", regex, s2, [pre evaluateWithObject:s2]?@"是移动号码":@"不是移动号码");
    NSLog(@"正则表达式:%@ %@ %@", regex, s3, [pre evaluateWithObject:s3]?@"是移动号码":@"不是移动号码");
    NSLog(@"正则表达式:%@ %@ %@", regex, s4, [pre evaluateWithObject:s4]?@"是移动号码":@"不是移动号码");
    NSLog(@"正则表达式:%@ %@ %@", regex, s5, [pre evaluateWithObject:s5]?@"是移动号码":@"不是移动号码");
    NSLog(@"正则表达式:%@ %@ %@", regex, s6, [pre evaluateWithObject:s6]?@"是移动号码":@"不是移动号码");
}


+ (void)is11Number {
    NSString *s1 = @"12433445";
    NSString *s2 = @"18566699879";
    NSString *s3 = @"12345678901";
    NSString *s4 = @"21324";
    NSString *regex = @"^[0-9]{11}";
    NSPredicate *pre = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    NSLog(@"正则表达式:%@ %@ %@", regex, s1, [pre evaluateWithObject:s1]?@"是11位数字":@"不是11位数字");
    NSLog(@"正则表达式:%@ %@ %@", regex, s2, [pre evaluateWithObject:s2]?@"是11位数字":@"不是11位数字");
    NSLog(@"正则表达式:%@ %@ %@", regex, s3, [pre evaluateWithObject:s3]?@"是11位数字":@"不是11位数字");
    NSLog(@"正则表达式:%@ %@ %@", regex, s4, [pre evaluateWithObject:s4]?@"是11位数字":@"不是11位数字");
}



#pragma mark- ==== 语法 ====


+ (void)once {
    NSString    *regex = @"^one";
    NSPredicate *pre   = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex]; //MATCHES
    NSString *s1 = @"one";
    NSString *s2 = @"onell";
    NSString *s3 = @"ooneone";
    NSString *s4 = @"oneone";
//    NSLog(@"正则表达式的意思: runo 加  n个 o 后 d");
    NSLog(@"正则表达式:%@  字符串:%@   %@", regex, s1, [pre evaluateWithObject:s1]? @"符合":@"不符合");
    NSLog(@"正则表达式:%@  字符串:%@   %@", regex, s2, [pre evaluateWithObject:s2]? @"符合":@"不符合");
    NSLog(@"正则表达式:%@  字符串:%@   %@", regex, s3, [pre evaluateWithObject:s3]? @"符合":@"不符合");
    NSLog(@"正则表达式:%@  字符串:%@   %@", regex, s4, [pre evaluateWithObject:s4]? @"符合":@"不符合");
}



//==== 例子 ====//

/** colou?r 可以匹配 color 或者 colour，? 问号代表前面的字符最多只可以出现一次（0次、或1次） */
+ (void)runoAskD {
    NSString *regex = @"^runo?d";
    NSPredicate *pre = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex]; //MATCHES
    NSString *s1 = @"arunod";
    NSString *s2 = @"rund";
    NSString *s3 = @"runoooood";
    NSString *s4 = @"runoed";
    NSLog(@"正则表达式的意思: runo 加  n个 o 后 d");
    NSLog(@"正则表达式:%@  字符串:%@   %@", regex, s1, [pre evaluateWithObject:s1]? @"符合":@"不符合");
    NSLog(@"正则表达式:%@  字符串:%@   %@", regex, s2, [pre evaluateWithObject:s2]? @"符合":@"不符合");
    NSLog(@"正则表达式:%@  字符串:%@   %@", regex, s3, [pre evaluateWithObject:s3]? @"符合":@"不符合");
    NSLog(@"正则表达式:%@  字符串:%@   %@", regex, s4, [pre evaluateWithObject:s4]? @"符合":@"不符合");
}

/** runoo*b，可以匹配 runob、runoob、runoooooob 等，* 号代表字符可以不出现，也可以出现一次或者多次（0次、或1次、或多次） */
+ (void)runoAnyD {
    NSString *regex = @"^runo*d";
    NSPredicate *pre = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex]; //MATCHES
    NSString *s1 = @"runod";
    NSString *s2 = @"runood";
    NSString *s3 = @"runoooood";
    NSString *s4 = @"runoooed";
    NSLog(@"正则表达式的意思: runo 加  n个 o 后 d");
    NSLog(@"正则表达式:%@  字符串:%@   %@", regex, s1, [pre evaluateWithObject:s1]? @"符合":@"不符合");
    NSLog(@"正则表达式:%@  字符串:%@   %@", regex, s2, [pre evaluateWithObject:s2]? @"符合":@"不符合");
    NSLog(@"正则表达式:%@  字符串:%@   %@", regex, s3, [pre evaluateWithObject:s3]? @"符合":@"不符合");
    NSLog(@"正则表达式:%@  字符串:%@   %@", regex, s4, [pre evaluateWithObject:s4]? @"符合":@"不符合");
}


/** runo+b，可以匹配 runob、runoob、runooooob 等，+ 号代表前面的字符必须至少出现一次（0次或多次）。 */
+ (void)runoAndD {
    NSString *regex = @"^runo+d";
    NSPredicate *pre = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex]; //MATCHES
    NSString *s1 = @"runod";
    NSString *s2 = @"runood";
    NSString *s3 = @"runoooood";
    NSString *s4 = @"runoooed";
    NSLog(@"正则表达式的意思: runo 加  n个 o 后 d");
    NSLog(@"正则表达式:%@  字符串:%@   %@", regex, s1, [pre evaluateWithObject:s1]? @"符合":@"不符合");
    NSLog(@"正则表达式:%@  字符串:%@   %@", regex, s2, [pre evaluateWithObject:s2]? @"符合":@"不符合");
    NSLog(@"正则表达式:%@  字符串:%@   %@", regex, s3, [pre evaluateWithObject:s3]? @"符合":@"不符合");
    NSLog(@"正则表达式:%@  字符串:%@   %@", regex, s4, [pre evaluateWithObject:s4]? @"符合":@"不符合");
}



#pragma mark- ==== 简介 ====

+ (void)huakuohao {
    //  ^[0-9]+abc$
    NSString *regex = @"^[0-9]+abc$";
    NSPredicate *pre = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex]; //MATCHES
    NSString *s1 = @"123abc";
    NSString *s2 = @"234567abc";
    NSString *s3 = @"123abcd";
    NSString *s4 = @"123asd";
    NSLog(@"正则表达式的意思: 多个数字 后 是  abc");
    NSLog(@"正则表达式:%@  字符串:%@   %@", regex, s1, [pre evaluateWithObject:s1]? @"符合":@"不符合");
    NSLog(@"正则表达式:%@  字符串:%@   %@", regex, s2, [pre evaluateWithObject:s2]? @"符合":@"不符合");
    NSLog(@"正则表达式:%@  字符串:%@   %@", regex, s3, [pre evaluateWithObject:s3]? @"符合":@"不符合");
    NSLog(@"正则表达式:%@  字符串:%@   %@", regex, s4, [pre evaluateWithObject:s4]? @"符合":@"不符合");
}















 



@end






